package com.hyphenate.easeui.widget.presenter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.BaseAdapter;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMFileMessageBody;
import com.hyphenate.chat.EMImageMessageBody;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.ui.EaseShowBigImageActivity;
import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.hyphenate.easeui.widget.ShowImagesDialog2;
import com.hyphenate.easeui.widget.chatrow.EaseChatRow;
import com.hyphenate.easeui.widget.chatrow.EaseChatRowImage;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangsong on 17-10-12.
 */

public class EaseChatImagePresenter extends EaseChatFilePresenter {

    private List<String> imageuri;
    private int position;
    private List<String> msgIds;

    @Override
    protected EaseChatRow onCreateChatRow(Context cxt, EMMessage message, int position, BaseAdapter adapter) {
        return new EaseChatRowImage(cxt, message, position, adapter);
    }

    @Override
    protected void handleReceiveMessage(final EMMessage message) {
        super.handleReceiveMessage(message);

        getChatRow().updateView(message);

        message.setMessageStatusCallback(new EMCallBack() {
            @Override
            public void onSuccess() {
                getChatRow().updateView(message);
            }

            @Override
            public void onError(int code, String error) {
                getChatRow().updateView(message);
            }

            @Override
            public void onProgress(int progress, String status) {
                getChatRow().updateView(message);
            }
        });
    }

    @Override
    public void onBubbleClick(EMMessage message) {
        EMImageMessageBody imgBody = (EMImageMessageBody) message.getBody();
        imageuri = new ArrayList<>();
        msgIds = new ArrayList<>();
        File file2 = new File(imgBody.getLocalUrl());
        if (file2.exists()) {
           // imageuri.add(imgBody.getLocalUrl());
            //  intent.putExtra("uri", uri);
        } else {
            // The local full size pic does not exist yet.
            // ShowBigImage needs to download it from the server
            // first
            String msgId = message.getMsgId();
          //  msgIds.add(message.getMsgId());
            // intent.putExtra("messageId", msgId);
            //  intent.putExtra("localUrl", imgBody.getLocalUrl());
        }
        if (message != null && message.direct() == EMMessage.Direct.RECEIVE){
            EMConversation conversation = EMClient.getInstance().chatManager().getConversation(message.getFrom(), EaseCommonUtils.getConversationType(1),true);
            if (null != conversation) {
                List<EMMessage> messages = conversation.getAllMessages();
                if (messages != null) {
                    for (EMMessage emMessage : messages) {
                        if (emMessage.getBody() instanceof EMImageMessageBody) {
                            EMImageMessageBody imgBody2 = (EMImageMessageBody) emMessage.getBody();
                            imageuri.add(imgBody2.getLocalUrl());
                                msgIds.add(emMessage.getMsgId());

                        }
                    }
                }
            }
        }else {
            EMConversation conversation = EMClient.getInstance().chatManager().getConversation(message.getTo(), EaseCommonUtils.getConversationType(1),true);
            if (null != conversation) {
                List<EMMessage> messages = conversation.getAllMessages();
                if (messages != null) {
                    for (EMMessage emMessage : messages) {
                        if (emMessage.getBody() instanceof EMImageMessageBody) {
                            EMImageMessageBody imgBody2 = (EMImageMessageBody) emMessage.getBody();
                            msgIds.add(emMessage.getMsgId());
                            imageuri.add(imgBody2.getLocalUrl());
                        }
                    }
                }
            }
        }


        if(EMClient.getInstance().getOptions().getAutodownloadThumbnail()){
            if(imgBody.thumbnailDownloadStatus() == EMFileMessageBody.EMDownloadStatus.FAILED){
                getChatRow().updateView(message);
                // retry download with click event of user
                EMClient.getInstance().chatManager().downloadThumbnail(message);
            }
        } else{
            if(imgBody.thumbnailDownloadStatus() == EMFileMessageBody.EMDownloadStatus.DOWNLOADING ||
                    imgBody.thumbnailDownloadStatus() == EMFileMessageBody.EMDownloadStatus.PENDING ||
                       imgBody.thumbnailDownloadStatus() == EMFileMessageBody.EMDownloadStatus.FAILED){
                // retry download with click event of user
                EMClient.getInstance().chatManager().downloadThumbnail(message);
                getChatRow().updateView(message);
                return;
            }
        }
       // Intent intent = new Intent(getContext(), EaseShowBigImageActivity.class);


        if(imageuri !=null&& imageuri.size()>0){
            for(int i = 0; i< imageuri.size(); i++){
                if(TextUtils.equals(imageuri.get(i),imgBody.getLocalUrl())){
                    position = i;
                }
            }
        }
        final ShowImagesDialog2 showImagesDialog = new ShowImagesDialog2(getContext(), "", imageuri,msgIds,position);
        showImagesDialog.show();
        File file = new File(imgBody.getLocalUrl());
        if (file.exists()) {
            Uri uri = Uri.fromFile(file);
          //  intent.putExtra("uri", uri);
        } else {
            // The local full size pic does not exist yet.
            // ShowBigImage needs to download it from the server
            // first
            String msgId = message.getMsgId();
           // intent.putExtra("messageId", msgId);
          //  intent.putExtra("localUrl", imgBody.getLocalUrl());
        }
        if (message != null && message.direct() == EMMessage.Direct.RECEIVE && !message.isAcked()
                && message.getChatType() == EMMessage.ChatType.Chat) {
            try {
                EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
       // getContext().startActivity(intent);
    }
}
